﻿using _2112A_2_MES._wangweihao;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.SuJiaHaos;
using _2112A_2_MES.SuJiaHaos.Sasles;
using _2112A_2_MES.SuJiaHaos.StockTakings;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.SuJiaHao
{
    /// <summary>
    /// 库存盘点记录表服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class StockTakingService : ApplicationService, IStockTakingService
    {
        /// <summary>
        /// 销售退货仓储
        /// </summary>
        private readonly IRepository<StockTaking, int> _stockTakingRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stockTakingRepository">销售退货仓储</param>
        public StockTakingService(IRepository<StockTaking, int> stockTakingRepository)
        {
            _stockTakingRepository = stockTakingRepository;
        }

        /// <summary>
        /// 获取库存盘点列表
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        public async Task<ReturnSelect<object>> GetStockTakingList(int pageSize, int pageIndex)
        {
            // 获取生产领料列表
            var stockTakingList = await _stockTakingRepository.GetListAsync();

            //// 使用 WhereIf 进行条件筛选
            //var wareHouseLists = wareHouseList
            //    .WhereIf(!string.IsNullOrEmpty(warehouseCode), x => x.WarehouseCode.Contains(warehouseCode))
            //    .WhereIf(!string.IsNullOrEmpty(warehouseName), x => x.WarehouseName.Contains(warehouseName))
            //    .ToList();

            // 计算总记录数和当前页记录
            var totalCount = stockTakingList.Count;
            var pageList = stockTakingList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<StockTaking>(pageList);

            //返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);
        }

        /// <summary>
        /// 库存盘点添加
        /// </summary>
        /// <param name="input">库存盘点实体</param>
        /// <returns></returns>
        public async Task<int> GetCreateStockTaking(CreateStockTakingDTO input)
        {
            var stockTakings = ObjectMapper.Map<CreateStockTakingDTO, StockTaking>(input);
            stockTakings.CreateBy = "崔二强";
            stockTakings.CreateTime = DateTime.Now;
            await _stockTakingRepository.InsertAsync(stockTakings);
            return stockTakings != null ? 1 : 0;
        }

        /// <summary>
        /// 库存盘点信息删除
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteStockTaking(int id)
        {
            var stockTakinDel = await _stockTakingRepository.GetAsync(id);
            await _stockTakingRepository.DeleteAsync(stockTakinDel);
            return stockTakinDel != null ? 1 : 0;
        }

        /// <summary>
        /// 库存盘点反填
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回库存盘点信息实体</returns>
        public async Task<StockTaking> GetStockTaking(int id)
        {
            var stockTakings = await _stockTakingRepository.FirstOrDefaultAsync(x => x.Id == id);
            return stockTakings;
        }

        /// <summary>
        /// 销售退货信息修改
        /// </summary>
        /// <param name="input">仓库实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateStockTaking(CreateStockTakingDTO input)
        {
            var stockTakings = ObjectMapper.Map<CreateStockTakingDTO,StockTaking>(input);
            stockTakings.UpdateBy = "崔大强";
            stockTakings.UpdateTime = DateTime.Now;
            await _stockTakingRepository.UpdateAsync(stockTakings);
            return stockTakings != null ? 1 : 0;
        }

    }
}
